﻿import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Matrix;

class VectorRect 
{		
	public function new(
		fillColors:Array<UInt>,
		fillAlphas:Array<Float>,
		fillRatios:Array<Float>,
		borderColors:Array<UInt>,
		borderAlphas:Array<Float>,
		borderRatios:Array<Float>,
		borderThickness:Float
	) 
	{
		this.fillColors = fillColors;
		this.fillAlphas = fillAlphas;
		this.fillRatios = fillRatios;
		this.borderColors = borderColors;
		this.borderAlphas = borderAlphas;
		this.borderRatios = borderRatios;
		this.borderThickness = borderThickness;
	}
	
	public var fillColors:Array<UInt>;
	
	public var fillAlphas:Array<Float>;
	
	public var fillRatios:Array<Float>;
	
	public var borderColors:Array<UInt>;
	
	public var borderAlphas:Array<Float>;
	
	public var borderRatios:Array<Float>;
	
	public var borderThickness:Float;
	
	static var _matrix:Matrix;
	
	public function draw(g:Graphics, x:Float, y:Float, width:Float, height:Float, radius:Float)
	{
		if (_matrix == null)
		{
			_matrix = new Matrix();
		}
		var matrix = _matrix;
		var border = borderThickness;
		var innerEllipse = (radius - border > 0 ? radius - border : 0) * 2;
		g.clear();
		matrix.createGradientBox(height, height, Math.PI * .5);
		g.beginGradientFill(GradientType.LINEAR, borderColors, borderAlphas, borderRatios, matrix);
		g.drawRoundRect(0, 0, width, height, radius * 2);
		g.drawRoundRect(border, border, width - border * 2, height - border * 2, innerEllipse);
		g.endFill();
		matrix.createGradientBox(height, height, Math.PI * .5);
		g.beginGradientFill(GradientType.LINEAR, fillColors, fillAlphas, fillRatios, matrix);
		g.drawRoundRect(border, border, width - border * 2, height - border * 2, innerEllipse);
		g.endFill();
	}
}